package com.sys.service.impl;

import java.sql.SQLException;

import com.sys.dao.UserDao;
import com.sys.dao.impl.UserDaoImpl;
import com.sys.domain.User;
import com.sys.service.UserService;

public class UserServiceImpl implements UserService {

	@Override
	public void addUser(User user) throws SQLException {
		if(user == null) {
			return;
		}
		UserDao userDao = new UserDaoImpl();
		userDao.add(user);
	}

	@Override
	public Boolean isLegalUserById(String userId) throws SQLException {
		if(userId == null) {
			return false;
		}
		UserDao userDao = new UserDaoImpl();
		return userDao.isUserIdLegal(userId);
	}

	@Override
	public Boolean findUserById(String userId) throws SQLException {
		if(userId == null) {
			return false;
		}
		UserDao userDao = new UserDaoImpl();
		return userDao.findUserByUserId(userId)!=null?true:false;
	}

	@Override
	public Boolean isEmailExist(String eMail) throws SQLException {
		if(eMail == null) {
			return false;
		}
		UserDao userDao = new UserDaoImpl();
		return userDao.isEmailExist(eMail);
	}

	@Override
	public Boolean checkUserIsExist(String str, String userPwd) throws SQLException {
		if(str==null||userPwd==null) 
			return false;
		String []vals = str.split("@");
		//能被@分隔为两个子串说明是邮箱
		if(vals.length != 2) {
			return new UserDaoImpl().findUserByIdAndPwd(str, userPwd);
		}
		return new UserDaoImpl().findUserByEmailAndPwd(str, userPwd);
	}

	@Override
	public String getUserIdByEmail(String eMail) throws SQLException {
		if(eMail == null) {
			return "******";
		}
		
		UserDao userDao = new UserDaoImpl();
		
		return userDao.getUserIdByEmailDao(eMail);
	}

	@Override
	public Boolean deleteUser(User user) throws SQLException {
		if(user == null) {
			return false;
		}
		
		UserDao userDao = new UserDaoImpl();
		
		return userDao.delete(user);
	}

	@Override
	public Boolean changeUserPwd(String userId, String eMail, String userPwd, String newPwd) throws SQLException {
		if(userId == null || eMail == null || userId == null|| newPwd == null) {
			return false;
		}
		UserDao uDao = new UserDaoImpl();
		Boolean b = uDao.checkUserInfoWhoChangePwd(userId, eMail, userPwd);
		if(b) {
			b = uDao.changeUserPwdByUserId(userId, newPwd);
		}
		return b;
	}

	
}
